<template>
  <div>
    <el-dialog
      :title="title"
      :visible="dialogVisible"
      :show-close="false"
      width="600px"
    >
      <el-form
        ref="parameterDialog"
        :model="row"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item
          v-show="!row.id"
          label="参数名称:"
          prop="key0"
          style="width: 350px;"
        >
          <el-input
            v-model="row.key0"
            size="medium"
            placeholder="请输入参数名称"
          />
        </el-form-item>
        <el-form-item
          v-show="row.id"
          label="参数名称:"
          style="width: 350px;"
        >
          <div>{{ row.key0 }}</div>
        </el-form-item>
        <el-form-item
          v-show="row.id"
          label="描述:"
          style="width: 350px;"
        >
          <div>{{ row.des }}</div>
        </el-form-item>
        <el-form-item
          label="参数:"
          prop="value"
          style="width: 350px;"
        >
          <el-input
            v-model="row.value"
            size="medium"
            placeholder="请输入参数"
          />
        </el-form-item>
        <el-form-item
          v-show="!row.id"
          label="描述:"
          prop="des"
          style="width: 350px;"
        >
          <el-input
            v-model="row.des"
            size="medium"
            placeholder="请输入描述"
          />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button
          @click="handleCancel"
        >
          取 消
        </el-button>
        <el-button
          type="primary"
          @click="handleConfirm"
        >
          确 定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import mixin from '@/mixin/mixin'

export default {
  name: 'ParameterDialog',
  mixins: [mixin],
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      title: '编辑',
      rules: {
        value: [
          {
            required: true,
            message: '参数不能为空',
            trigger: 'blur'
          }
        ],
        key0: [
          {
            required: true,
            message: '参数名称不能为空',
            trigger: 'blur'
          }
        ],
        des: [
          {
            required: true,
            message: '描述不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  updated() {
    if (this.row.id) {
      this.title = '编辑'
    } else {
      this.title = '新增'
    }
  },
  methods: {
    handleCancel() {
      this.$emit('cancel-dialog')
      this.$refs.parameterDialog.resetFields()
    },
    handleConfirm() {
      this.$refs.parameterDialog.validate((valid) => {
        if (valid) {
          this.$emit('confirm-dialog', {
            // dialog: this.$refs.garageDialog,
            id: this.row.id,
            key0: this.row.key0,
            value: this.row.value,
            des: this.row.des
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style>
</style>
